#include "../../lvgl/lvgl.h"
#include "ui.h"
  
lv_obj_t * ui_page1 = NULL;

static pthread_mutex_t mutex;

static int msg_fd = 0;
static int light_fd = 0;
static int fd_Red1 = 0;
static int fd_Red2 = 0;
static int fd_Red3 = 0;
static int fd_Red4 = 0;
static int last_humi = -1, last_light = -1;
static int read_interval = 1000;

lv_obj_t * Home = NULL;
lv_obj_t * Sleep = NULL;
lv_obj_t * Windy = NULL;
lv_obj_t * Auto = NULL;

lv_obj_t * Switch1 = NULL;
lv_obj_t * Switch2 = NULL;
lv_obj_t * Switch3 = NULL;
lv_obj_t * Switch4 = NULL;

lv_obj_t * kongtiao_arc = NULL;
lv_obj_t * led_arc = NULL;
lv_obj_t * chuanglian_slider = NULL;

lv_obj_t * kongtiao_label = NULL;
lv_obj_t * led_label = NULL;
lv_obj_t * chuanglian_label = NULL;

lv_obj_t * temperature = NULL;
lv_obj_t * humidity = NULL;
lv_obj_t * light = NULL;
int temperature_value = 26;
int humidity_value = 40;
int light_value = 50;

lv_obj_t * temperature_label = NULL;
lv_obj_t * humidity_label = NULL;
lv_obj_t * light_label = NULL;

static void event_Demo(lv_event_t *e)
{
    lv_scr_load(ui_page2);
}

static void event_exit(lv_event_t *e)
{
    //关闭所有家电
    lv_obj_clear_state(Switch1, LV_STATE_CHECKED);
    lv_obj_clear_state(Switch2, LV_STATE_CHECKED);
    lv_obj_clear_state(Switch3, LV_STATE_CHECKED);
    lv_obj_clear_state(Switch4, LV_STATE_CHECKED);
    lv_arc_set_value(kongtiao_arc , 16);
    lv_label_set_text(kongtiao_label, "16°C"); 
    lv_arc_set_value(led_arc , 0);
    lv_label_set_text(led_label, "0%");
    lv_slider_set_value(chuanglian_slider , 0 , LV_ANIM_ON);
    lv_label_set_text(chuanglian_label, "0%");
    lv_obj_clear_state(Home, LV_STATE_CHECKED);
    lv_obj_clear_state(Sleep, LV_STATE_CHECKED);
    lv_obj_clear_state(Windy, LV_STATE_CHECKED);
    lv_obj_set_style_bg_color(Home, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(Sleep, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(Windy, lv_color_hex(0x2b9bf7), LV_STATE_DEFAULT);

    Home_flag = 0;
    Sleep_flag = 0;
    Windy_flag = 0;
    Auto_flag = 0;

    fd_0_Fun(fd_Red1); 
    fd_0_Fun(fd_Red2); 
    fd_0_Fun(fd_Red3); 
    fd_0_Fun(fd_Red4);

    user_value_modify_init();

    lv_textarea_set_text(ta_ID, "");
    lv_textarea_set_text(ta_Passwd, "");

    lv_scr_load(ui_page);
}

//通过旋钮的值来设定湿度的值，用LVGL定时器实时获取旋钮的值
static void timer_cb(lv_timer_t *timer) {
    (void)timer;  // 避免未使用参数警告
    
    //旋钮部分
    
    // 快速读取原始值
    char msg[16] = {0};
    lseek(msg_fd, 0, SEEK_SET);
    bzero( msg , sizeof(msg) );
    int retVal = read( msg_fd , msg , sizeof(msg) );
    if( retVal <= 0 )
        perror("read error");

    humidity_value = (atoi(msg) - 1) * 100 / 1022;

    // 更新UI
    if (humidity_value != last_humi)
    {
        if(humidity_value > 60 && Auto_flag == 1)
                lv_obj_clear_state(Switch4, LV_STATE_CHECKED);
        if(humidity_value < 30 && Auto_flag == 1)
            lv_obj_add_state(Switch4, LV_STATE_CHECKED);
        lv_slider_set_value(humidity , humidity_value , LV_ANIM_ON);
        lv_label_set_text_fmt(humidity_label, "%d%%", humidity_value);
        last_humi = humidity_value;
    }

    //光照部分
    // 快速读取原始值
    char Light_Value[16] = {0};
    lseek(light_fd, 0, SEEK_SET);
    bzero( Light_Value , sizeof(Light_Value) );
    retVal = read( light_fd , Light_Value , sizeof(Light_Value) );
    if( retVal <= 0 )
        perror("read error");

    light_value = atoi(Light_Value) / 2;
    
    // 更新UI
    if(light_value != last_light)
    {
        last_light = light_value;

        if(light_value < 20 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 100);
            lv_label_set_text(led_label, "100%");
            fd_1_Fun(fd_Red1); 
            fd_1_Fun(fd_Red2); 
            fd_1_Fun(fd_Red3); 
            fd_1_Fun(fd_Red4); 
        }
        else if(light_value >= 20 && light_value < 40 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 75);
            lv_label_set_text(led_label, "75%");
            fd_1_Fun(fd_Red1); 
            fd_1_Fun(fd_Red2); 
            fd_1_Fun(fd_Red3); 
            fd_0_Fun(fd_Red4); 
        }
        else if(light_value >= 40 && light_value < 60 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 50);
            lv_label_set_text(led_label, "50%");
            fd_1_Fun(fd_Red1); 
            fd_1_Fun(fd_Red2); 
            fd_0_Fun(fd_Red3); 
            fd_0_Fun(fd_Red4); 
        }
        else if(light_value >= 60 && light_value < 80 && Auto_flag == 1)
        {
            lv_obj_add_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 25);
            lv_label_set_text(led_label, "25%");
            fd_1_Fun(fd_Red1); 
            fd_0_Fun(fd_Red2); 
            fd_0_Fun(fd_Red3); 
            fd_0_Fun(fd_Red4); 
        }
        else if(light_value >= 80 && Auto_flag == 1)
        {
            lv_obj_clear_state(Switch2, LV_STATE_CHECKED);
            lv_arc_set_value(led_arc , 0);
            lv_label_set_text(led_label, "0%");
            fd_0_Fun(fd_Red1); 
            fd_0_Fun(fd_Red2); 
            fd_0_Fun(fd_Red3); 
            fd_0_Fun(fd_Red4); 
        }
        lv_slider_set_value(light , light_value , LV_ANIM_ON);
        lv_label_set_text_fmt(light_label, "%d%%", light_value);
    }

    //温度部分，使用select来监视文件描述符的状态，确保在有数据可读时才执行read_adc_key和其他读取操作
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(adc_key_fd, &readfds);

    int max_fd = adc_key_fd;

    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 0; // 立即返回

    int ret = select(max_fd + 1, &readfds, NULL, NULL, &timeout);
    if (ret == -1) {
        perror("select error");
        return;
    }

    if (FD_ISSET(adc_key_fd, &readfds)) {
        pthread_mutex_lock(&mutex);
        read_adc_key();
        pthread_mutex_unlock(&mutex);
    }
}

void Page_1()
{
    CN_Freetype();

    //创建主屏幕
    // ui_page1 = lv_scr_act();
    ui_page1 = lv_obj_create(NULL);
    lv_obj_clear_flag(ui_page1, LV_OBJ_FLAG_SCROLLABLE); //清除滚动标志

    //打开获取旋钮值的文件
    msg_fd = open(DEV_PATH, O_RDONLY);
    if(msg_fd < 0) {
        perror("Failed to open DEV_PATH");
        return;
    }
    //打开灯光文件
    led_init();
    fd_Red1 = open( RED1 , O_WRONLY); 
    fd_Red2 = open( RED2 , O_WRONLY);    
    fd_Red3 = open( RED3 , O_WRONLY);
    fd_Red4 = open( RED4 , O_WRONLY); 
    //打开光强传感器文件
    light_fd = open(Light_PATH, O_RDONLY);
    if(light_fd < 0) {
        perror("Failed to open Light_PATH");
        return;
    }

    //创建标题标签
    lv_obj_t * Title = lv_label_create(ui_page1);
    lv_obj_set_width(Title, lv_pct(50));
    lv_obj_set_height(Title, lv_pct(15));
    lv_obj_set_x(Title, lv_pct(-10));
    lv_obj_set_y(Title, lv_pct(2));
    lv_obj_set_align(Title , LV_ALIGN_TOP_MID);
    lv_obj_add_style(Title, &style_50, 0);             //给标签添加字体风格
    lv_label_set_text(Title, "智能家居中控");


    //创建切换到手动模式的按钮，设定好标签文本以及回调函数
    lv_obj_t * Demo = lv_btn_create(ui_page1);
    lv_obj_set_width(Demo, lv_pct(20));
    lv_obj_set_height(Demo, lv_pct(10));
    lv_obj_set_x(Demo, lv_pct(-10));
    lv_obj_set_y(Demo, lv_pct(5));
    lv_obj_set_align(Demo, LV_ALIGN_TOP_RIGHT);
    //添加一个标志，使其在获得焦点时能够滚动。这通常用于确保当按钮获得焦点时，可以通过滚动来查看或点击它。
    lv_obj_add_flag(Demo, LV_OBJ_FLAG_SCROLL_ON_FOCUS);   
    lv_obj_clear_flag(Demo, LV_OBJ_FLAG_SCROLLABLE);  //清除滚动标志
    lv_obj_t * Demo_label = lv_label_create(Demo);
    lv_obj_set_width(Demo_label, lv_pct(100));
    lv_obj_set_height(Demo_label, lv_pct(100));
    lv_obj_set_align(Demo_label , LV_ALIGN_CENTER);
    lv_obj_add_style(Demo_label, &style_32, 0); //给标签添加字体风格
    lv_label_set_text(Demo_label, "模式设置");
    lv_obj_add_event_cb(Demo, event_Demo, LV_EVENT_CLICKED, NULL);

    //创建退出的按钮，设定好标签文本以及回调函数
    lv_obj_t * exit = lv_btn_create(ui_page1);
    lv_obj_set_width(exit, lv_pct(15));
    lv_obj_set_height(exit, lv_pct(10));
    lv_obj_set_x(exit, lv_pct(2));
    lv_obj_set_y(exit, lv_pct(5));
    lv_obj_t * exit_label = lv_label_create(exit);
    lv_obj_set_width(exit_label, lv_pct(100));
    lv_obj_set_height(exit_label, lv_pct(100));
    lv_obj_set_align(exit_label , LV_ALIGN_CENTER);
    lv_obj_add_style(exit_label, &style_32, 0); //给标签添加字体风格
    lv_label_set_text(exit_label, "退出");
    lv_obj_add_event_cb(exit, event_exit, LV_EVENT_CLICKED, NULL);

    //创建场景选择区域
    lv_obj_t * area1 = lv_obj_create(ui_page1);
    lv_obj_set_width(area1, lv_pct(60));
    lv_obj_set_height(area1, lv_pct(80));
    lv_obj_set_x(area1, lv_pct(2));
    lv_obj_set_y(area1, lv_pct(7));
    lv_obj_set_align(area1, LV_ALIGN_LEFT_MID);
    lv_obj_clear_flag(area1, LV_OBJ_FLAG_SCROLLABLE);   //清除滚动标志

    lv_obj_t * container = lv_obj_create(area1);
    lv_obj_set_size(container, lv_pct(100), lv_pct(25));
    lv_obj_align(container, LV_ALIGN_TOP_MID, 0 , -15);
    lv_obj_set_scroll_dir(container, LV_DIR_HOR);   //启动水平滚动功能
    lv_obj_set_scrollbar_mode(container, LV_SCROLLBAR_MODE_AUTO);

    //创建回家模式按钮，设定好标签文本以及回调函数
    Home = lv_btn_create(container);
    lv_obj_set_width(Home, lv_pct(35));
    lv_obj_set_height(Home, lv_pct(100));
    lv_obj_set_align(Home, LV_ALIGN_TOP_LEFT);
    lv_obj_clear_flag(Home, LV_OBJ_FLAG_SCROLLABLE);    //清除滚动标志
    lv_obj_t * Home_label = lv_label_create(Home);
    lv_obj_set_width(Home_label, lv_pct(100));
    lv_obj_set_height(Home_label, lv_pct(100));
    lv_obj_set_align(Home_label , LV_ALIGN_CENTER);
    lv_obj_add_style(Home_label, &style_32, 0); //给标签添加字体风格
    lv_label_set_text(Home_label, "回家模式");
    lv_obj_add_event_cb(Home, event_Home, LV_EVENT_CLICKED, NULL);

    //创建睡眠模式按钮，设定好标签文本以及回调函数
    Sleep = lv_btn_create(container);
    lv_obj_set_width(Sleep, lv_pct(35));
    lv_obj_set_height(Sleep, lv_pct(100));
    lv_obj_align(Sleep, LV_ALIGN_TOP_MID, lv_pct(5), 0);
    lv_obj_clear_flag(Sleep, LV_OBJ_FLAG_SCROLLABLE);    //清除滚动标志
    lv_obj_t * Sleep_label = lv_label_create(Sleep);
    lv_obj_set_width(Sleep_label, lv_pct(100));
    lv_obj_set_height(Sleep_label, lv_pct(100));
    lv_obj_set_align(Sleep_label , LV_ALIGN_CENTER);
    lv_obj_add_style(Sleep_label, &style_32, 0); //给标签添加字体风格
    lv_label_set_text(Sleep_label, "睡眠模式");
    lv_obj_add_event_cb(Sleep, event_Sleep, LV_EVENT_CLICKED, NULL);

    //创建通风模式按钮，设定好标签文本以及回调函数
    Windy = lv_btn_create(container);
    lv_obj_set_width(Windy, lv_pct(35));
    lv_obj_set_height(Windy, lv_pct(100));
    lv_obj_align(Windy, LV_ALIGN_TOP_RIGHT, lv_pct(10), 0);
    lv_obj_clear_flag(Windy, LV_OBJ_FLAG_SCROLLABLE);    //清除滚动标志
    lv_obj_t * Windy_label = lv_label_create(Windy);
    lv_obj_set_width(Windy_label, lv_pct(100));
    lv_obj_set_height(Windy_label, lv_pct(100));
    lv_obj_set_align(Windy_label , LV_ALIGN_CENTER);
    lv_obj_add_style(Windy_label, &style_32, 0); //给标签添加字体风格
    lv_label_set_text(Windy_label, "通风模式");
    lv_obj_add_event_cb(Windy, event_Windy, LV_EVENT_CLICKED, NULL);

    //创建自动模式按钮，设定好标签文本以及回调函数
    Auto = lv_btn_create(container);
    lv_obj_set_width(Auto, lv_pct(35));
    lv_obj_set_height(Auto, lv_pct(100));
    lv_obj_align(Auto, LV_ALIGN_TOP_RIGHT, lv_pct(50), 0);
    lv_obj_clear_flag(Auto, LV_OBJ_FLAG_SCROLLABLE);    //清除滚动标志
    lv_obj_t * Auto_label = lv_label_create(Auto);
    lv_obj_set_width(Auto_label, lv_pct(100));
    lv_obj_set_height(Auto_label, lv_pct(100));
    lv_obj_set_align(Auto_label , LV_ALIGN_CENTER);
    lv_obj_add_style(Auto_label, &style_32, 0); //给标签添加字体风格
    lv_label_set_text(Auto_label, "自动模式");
    lv_obj_add_event_cb(Auto, event_Auto, LV_EVENT_CLICKED, NULL);

    //创建一个内置区域，显示各种家电的状态
    lv_obj_t * In_area = lv_obj_create(area1);
    lv_obj_set_width(In_area, lv_pct(90));
    lv_obj_set_height(In_area, lv_pct(80));
    lv_obj_set_x(In_area, lv_pct(0));
    lv_obj_set_y(In_area, lv_pct(23));
    lv_obj_set_align(In_area, LV_ALIGN_TOP_MID);
    lv_obj_clear_flag(In_area, LV_OBJ_FLAG_SCROLLABLE);     //清除滚动标志
    
    //创建空调标签，表明下面按钮开关是控制空调
    lv_obj_t * kongtiao = lv_label_create(In_area);
    lv_obj_set_width(kongtiao, lv_pct(20));
    lv_obj_set_height(kongtiao, lv_pct(10));
    lv_obj_set_x(kongtiao, lv_pct(0));
    lv_obj_set_y(kongtiao, lv_pct(6));
    lv_obj_add_style(kongtiao, &style_32, 0);  
    lv_label_set_text(kongtiao, "空调");

    //创建灯光标签，表明下面按钮开关是控制灯光
    lv_obj_t * LED = lv_label_create(In_area);
    lv_obj_set_width(LED, lv_pct(20));
    lv_obj_set_height(LED, lv_pct(10));
    lv_obj_set_x(LED, lv_pct(25));
    lv_obj_set_y(LED, lv_pct(6));
    lv_obj_add_style(LED, &style_32, 0);  
    lv_label_set_text(LED, "灯光");

    //创建窗帘标签，表明下面按钮开关是控制窗帘
    lv_obj_t * chuanglian = lv_label_create(In_area);
    lv_obj_set_width(chuanglian, lv_pct(20));
    lv_obj_set_height(chuanglian, lv_pct(10));
    lv_obj_set_x(chuanglian, lv_pct(50));
    lv_obj_set_y(chuanglian, lv_pct(6));
    lv_obj_add_style(chuanglian, &style_32, 0);  
    lv_label_set_text(chuanglian, "窗帘");

    //创建加湿器标签，表明下面按钮开关是控制加湿器
    lv_obj_t * jiashiqi = lv_label_create(In_area);
    lv_obj_set_width(jiashiqi, lv_pct(30));
    lv_obj_set_height(jiashiqi, lv_pct(10));
    lv_obj_set_x(jiashiqi, lv_pct(75));
    lv_obj_set_y(jiashiqi, lv_pct(6));
    lv_obj_add_style(jiashiqi, &style_32, 0);  
    lv_label_set_text(jiashiqi, "加湿器");

    //创建四个开关按钮，分别控制四个电器的开关
    Switch1 = lv_switch_create(In_area);
    lv_obj_set_width(Switch1, lv_pct(10));
    lv_obj_set_height(Switch1, lv_pct(8));
    lv_obj_set_x(Switch1, lv_pct(5));
    lv_obj_set_y(Switch1, lv_pct(20));
    lv_obj_add_event_cb(Switch1, switch_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    Switch2 = lv_switch_create(In_area);
    lv_obj_set_width(Switch2, lv_pct(10));
    lv_obj_set_height(Switch2, lv_pct(8));
    lv_obj_set_x(Switch2, lv_pct(30));
    lv_obj_set_y(Switch2, lv_pct(20));
    lv_obj_add_event_cb(Switch2, switch_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    Switch3 = lv_switch_create(In_area);
    lv_obj_set_width(Switch3, lv_pct(10));
    lv_obj_set_height(Switch3, lv_pct(8));
    lv_obj_set_x(Switch3, lv_pct(55));
    lv_obj_set_y(Switch3, lv_pct(20));
    lv_obj_add_event_cb(Switch3, switch_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    Switch4 = lv_switch_create(In_area);
    lv_obj_set_width(Switch4, lv_pct(10));
    lv_obj_set_height(Switch4, lv_pct(8));
    lv_obj_set_x(Switch4, lv_pct(84));
    lv_obj_set_y(Switch4, lv_pct(20));

    //创建一个弧形，用来显现空调设置的温度
    kongtiao_arc = lv_arc_create(In_area);
    lv_obj_set_width(kongtiao_arc, lv_pct(30));
    lv_obj_set_height(kongtiao_arc, lv_pct(50));
    lv_obj_set_x(kongtiao_arc, lv_pct(-35));
    lv_obj_set_y(kongtiao_arc, lv_pct(16));
    lv_obj_set_align(kongtiao_arc, LV_ALIGN_CENTER);
    lv_arc_set_mode(kongtiao_arc, LV_ARC_MODE_NORMAL);
    lv_arc_set_range(kongtiao_arc, 16 , 40);
    lv_arc_set_value(kongtiao_arc , 16);
    lv_obj_add_event_cb(kongtiao_arc, arc_value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_obj_t * kongtiao_state = lv_label_create(In_area);
    lv_obj_set_width(kongtiao_state, lv_pct(30));
    lv_obj_set_height(kongtiao_state, lv_pct(10));
    lv_obj_align_to(kongtiao_state, kongtiao_arc, LV_ALIGN_OUT_BOTTOM_MID, 0, -20);
    lv_obj_add_style(kongtiao_state, &style_32, 0);  
    lv_label_set_text(kongtiao_state, "空调温度");

    kongtiao_label = lv_label_create(kongtiao_arc);
    lv_obj_set_width(kongtiao_label, lv_pct(60));
    lv_obj_set_height(kongtiao_label, lv_pct(50));
    lv_obj_set_x(kongtiao_label, lv_pct(15));
    lv_obj_set_y(kongtiao_label, lv_pct(10));
    lv_obj_set_align(kongtiao_label, LV_ALIGN_CENTER);
    lv_obj_set_style_text_font(kongtiao_label, &lv_font_montserrat_32 , 0);
    lv_label_set_text(kongtiao_label, "16°C"); // 初始值

    //创建一个弧形，用来显现灯光强度
    led_arc = lv_arc_create(In_area);
    lv_obj_set_width(led_arc, lv_pct(30));
    lv_obj_set_height(led_arc, lv_pct(50));
    lv_obj_set_x(led_arc, lv_pct(-1));
    lv_obj_set_y(led_arc, lv_pct(16));
    lv_obj_set_align(led_arc, LV_ALIGN_CENTER);
    lv_arc_set_range(led_arc, 0 , 100);
    lv_arc_set_value(led_arc , 0);
    lv_obj_add_event_cb(led_arc, arc_value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_obj_t * led_state = lv_label_create(In_area);
    lv_obj_set_width(led_state, lv_pct(30));
    lv_obj_set_height(led_state, lv_pct(10));
    lv_obj_align_to(led_state, led_arc, LV_ALIGN_OUT_BOTTOM_MID, 0, -20);
    lv_obj_add_style(led_state, &style_32, 0);  
    lv_label_set_text(led_state, "灯光强度");

    led_label = lv_label_create(led_arc);
    lv_obj_set_width(led_label, lv_pct(60));
    lv_obj_set_height(led_label, lv_pct(50));
    lv_obj_set_x(led_label, lv_pct(10));
    lv_obj_set_y(led_label, lv_pct(10));
    lv_obj_set_align(led_label, LV_ALIGN_CENTER);
    lv_obj_set_style_text_font(led_label, &lv_font_montserrat_32 , 0);
    lv_label_set_text(led_label, "0%"); // 初始值

    //创建一个滑杆，用来记录窗帘打开的程度
    chuanglian_slider = lv_slider_create(In_area);
    lv_obj_set_width(chuanglian_slider, lv_pct(30));
    lv_obj_set_height(chuanglian_slider, lv_pct(5));
    lv_obj_set_x(chuanglian_slider, lv_pct(35));
    lv_obj_set_y(chuanglian_slider, lv_pct(10));
    lv_obj_set_align(chuanglian_slider, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(chuanglian_slider, slider_value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_obj_t * chuanglian_state = lv_label_create(In_area);
    lv_obj_set_width(chuanglian_state, lv_pct(30));
    lv_obj_set_height(chuanglian_state, lv_pct(30));
    lv_obj_align_to(chuanglian_state, chuanglian_slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_add_style(chuanglian_state, &style_32, 0);  
    lv_label_set_text(chuanglian_state, "窗帘开关程度");

    chuanglian_label = lv_label_create(In_area);
    lv_obj_set_width(chuanglian_label, lv_pct(20));
    lv_obj_set_height(chuanglian_label, lv_pct(10));
    lv_obj_align_to(chuanglian_label, chuanglian_slider ,LV_ALIGN_OUT_TOP_MID, 20, -10);
    lv_obj_set_style_text_font(chuanglian_label, &lv_font_montserrat_32 , 0);
    lv_label_set_text(chuanglian_label, "0%"); // 初始值

    //创建温湿度光照参数区域
    lv_obj_t * area2 = lv_obj_create(ui_page1);
    lv_obj_set_width(area2, lv_pct(35));
    lv_obj_set_height(area2, lv_pct(80));
    lv_obj_set_x(area2, lv_pct(-2));
    lv_obj_set_y(area2, lv_pct(7));
    lv_obj_set_align(area2, LV_ALIGN_RIGHT_MID);
    lv_obj_clear_flag(area2, LV_OBJ_FLAG_SCROLLABLE);

    //创建三个滑杆，分别对应温度、湿度、光照
    temperature = lv_slider_create(area2);
    lv_obj_set_width(temperature, lv_pct(10));
    lv_obj_set_height(temperature, lv_pct(70));
    lv_obj_set_x(temperature, lv_pct(10));
    lv_obj_set_y(temperature, lv_pct(-5));
    lv_obj_set_align(temperature, LV_ALIGN_LEFT_MID);
    lv_slider_set_range(temperature, 0 , 50);
    lv_slider_set_value(temperature , temperature_value , LV_ANIM_ON);
    lv_obj_add_event_cb(temperature, slider_value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    
    lv_obj_t * temperature_txt = lv_label_create(area2);
    lv_obj_set_width(temperature_txt, lv_pct(10));
    lv_obj_set_height(temperature_txt, lv_pct(30));
    lv_obj_align_to(temperature_txt, temperature, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_add_style(temperature_txt, &style_32, 0);  
    lv_label_set_text(temperature_txt, "温度");

    temperature_label = lv_label_create(area2);
    lv_obj_set_width(temperature_label, lv_pct(30));
    lv_obj_set_height(temperature_label, lv_pct(10));
    lv_obj_align_to(temperature_label, temperature ,LV_ALIGN_OUT_TOP_MID, 5, -5);
    lv_obj_set_style_text_font(temperature_label, &lv_font_montserrat_32 , 0);
    lv_label_set_text_fmt(temperature_label, "%d°C", temperature_value); // 初始值

    humidity = lv_slider_create(area2);
    lv_obj_set_width(humidity, lv_pct(10));
    lv_obj_set_height(humidity, lv_pct(70));
    lv_obj_set_x(humidity, lv_pct(40));
    lv_obj_set_y(humidity, lv_pct(-5));
    lv_obj_set_align(humidity, LV_ALIGN_LEFT_MID);
    lv_slider_set_value(humidity , humidity_value , LV_ANIM_ON);
    lv_obj_add_event_cb(humidity, slider_value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_obj_t * humidity_txt = lv_label_create(area2);
    lv_obj_set_width(humidity_txt, lv_pct(10));
    lv_obj_set_height(humidity_txt, lv_pct(30));
    lv_obj_align_to(humidity_txt, humidity, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_add_style(humidity_txt, &style_32, 0);  
    lv_label_set_text(humidity_txt, "湿度");

    humidity_label = lv_label_create(area2);
    lv_obj_set_width(humidity_label, lv_pct(30));
    lv_obj_set_height(humidity_label, lv_pct(10));
    lv_obj_align_to(humidity_label, humidity ,LV_ALIGN_OUT_TOP_MID, 5, -5);
    lv_obj_set_style_text_font(humidity_label, &lv_font_montserrat_32 , 0);
    lv_label_set_text_fmt(humidity_label, "%d%%", humidity_value); // 初始值

    light = lv_slider_create(area2);
    lv_obj_set_width(light, lv_pct(10));
    lv_obj_set_height(light, lv_pct(70));
    lv_obj_set_x(light, lv_pct(70));
    lv_obj_set_y(light, lv_pct(-5));
    lv_obj_set_align(light, LV_ALIGN_LEFT_MID);
    lv_slider_set_value(light , light_value , LV_ANIM_ON);
    lv_obj_add_event_cb(light, slider_value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_obj_t * light_txt = lv_label_create(area2);
    lv_obj_set_width(light_txt, lv_pct(10));
    lv_obj_set_height(light_txt, lv_pct(30));
    lv_obj_align_to(light_txt, light, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_add_style(light_txt, &style_32, 0);  
    lv_label_set_text(light_txt, "光照");

    light_label = lv_label_create(area2);
    lv_obj_set_width(light_label, lv_pct(30));
    lv_obj_set_height(light_label, lv_pct(10));
    lv_obj_align_to(light_label, light ,LV_ALIGN_OUT_TOP_MID, 5, -5);
    lv_obj_set_style_text_font(light_label, &lv_font_montserrat_32 , 0);
    lv_label_set_text_fmt(light_label, "%d%%", light_value); // 初始值

    lv_timer_create(timer_cb, read_interval, NULL);
}